<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/15 11:48
 * Desc: 发票服务
 */
class  Service_Api_Member_Invoice extends Contract_Api_Member_Invoice
{
    private $table_name = 'member_invoice';

    private $fields = array('id', 'title', 'type', 'taxpayer_number', 'taxpayer_address', 'taxpayer_phone', 'bank_branch', 'bank_account');

    public function list(int $mid, array $params = array()): array
    {
        /**
         * @var $page
         * @var $pagesize
         */
        extract($params);

        $builder = DB::select_array($this->fields)
            ->from($this->table_name)
            ->where('memberid', '=', $mid);

        if (isset($params['type'])) {
            $types = explode(',', $params['type']);
            if (count($types) > 1) {
                $builder->where('type', 'in', $types);
            } else {
                $builder->where('type', '=', $params['type']);
            }
        }
        $total = $builder->execute()->count();
        $builder->order_by('id', 'desc');
        if (!empty($page) && !empty($pagesize)) {
            $offset = St_Page::offset($page, $pagesize);
            $builder->offset($offset)->limit($pagesize);
        }

        $list = $builder->execute()->as_array();
        return array(
            'total' => $total,
            'list' => $list
        );
    }


    public function info(int $mid, int $id)
    {
        $info = DB::select_array($this->fields)
            ->from($this->table_name)
            ->where('memberid', '=', $mid)
            ->where('id', '=', $id)
            ->execute()
            ->current();
        return $info ?: '';
    }


    public function edit(int $mid, array $params = array()): array
    {
        /**
         * @var $title
         * @var $type
         * @var $title
         * @var $taxpayer_number
         * @var $taxpayer_address
         * @var $taxpayer_phone
         * @var $bank_branch
         * @var $bank_account
         */
        extract($params);

        if (!empty($id)) {
            $info = ORM::factory($this->table_name, $id)
                ->where('memberid', '=', $mid);
            if (!$info->id) {
                throw new Exception('信息不存在');
            }
        } else {
            $info = ORM::factory($this->table_name);
        }
        $info->memberid         = $mid;
        $info->type             = $type;
        $info->title            = $title;
        $info->taxpayer_number  = '';
        $info->taxpayer_address = '';
        $info->taxpayer_phone   = '';
        $info->bank_branch      = '';
        $info->bank_account     = '';
        // 公司类型-纳税人识别号
        if (in_array($type, array(Model_Api_V2_Member_Invoice::TYPE_COMPANY, Model_Api_V2_Member_Invoice::TYPE_VAT))) {
            $info->taxpayer_number = $taxpayer_number;
        }

        // 增值税发票
        if ($type == Model_Api_V2_Member_Invoice::TYPE_VAT) {
            $info->taxpayer_address = $taxpayer_address;
            $info->taxpayer_phone   = $taxpayer_phone;
            $info->bank_branch      = $bank_branch;
            $info->bank_account     = $bank_account;
        }

        $info->save();
        if ($info->saved()) {
            $info->reload();
            return array('id' => $info->id);
        } else {
            throw new Exception('保存失败');
        }
    }


    public function delete(int $mid, int $id): bool
    {
        $row = DB::delete($this->table_name)
            ->where('memberid', '=', $mid)
            ->where('id', '=', $id)
            ->execute();
        return (bool)$row;
    }

    public function get_order_invoice(int $type_id): array
    {
        // 检查该产品类型是否开启发票
        if ($type_id == 14) {
            $open        = Model_Sysconfig::get_sys_conf('value', 'cfg_plugin_customize_usebill'); // 是否开启发票填写
            $open        = $open ?: 0;
            $contents    = '旅游费'; // 发票明细
            $description = ''; // 发票说明
            $types       = '0,1,2'; // 发票类型
        } else {
            $open_key    = 'cfg_invoice_open_' . $type_id;
            $content_key = 'cfg_invoice_content_' . $type_id;
            $type_key    = 'cfg_invoice_type_' . $type_id;
            $des_key     = 'cfg_invoice_des_' . $type_id;

            $fields      = array(
                $open_key,
                $content_key,
                $des_key,
                $type_key
            );
            $configs     = Model_Sysconfig::get_configs(0, $fields);
            $open        = $configs[$open_key] ?? 0;
            $contents    = $configs[$content_key] ?? '';
            $types       = $configs[$type_key] ?? '';
            $description = $configs[$des_key] ?? '';
        }
        $contents = explode(',', $contents);
        $types    = explode(',', $types);
        return array(
            'is_open' => $open,
            'contents' => $contents,
            'types' => $types,
            'description' => $description
        );
    }
}
